package com.example.li_han.servlet;

import com.example.li_han.service.UserService;
import com.example.li_han.pojo.User;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

// LoginServlet.java - 用户登录
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("UTF-8");

        BufferedReader br = req.getReader();
        StringBuilder sbuffer = new StringBuilder();
        String s;

        Map<String,Object> map = new HashMap<>();
        Gson gson = new Gson();

        try {
            while((s = br.readLine()) != null) {
                sbuffer.append(s);
            }
            System.out.println("普通用户登录请求: " + sbuffer.toString());

            // 解析JSON数据
            Map requestMap = gson.fromJson(sbuffer.toString(), Map.class);
            String zhangHao = (String) requestMap.get("zhangHao");
            String password = (String) requestMap.get("password");

            if (zhangHao == null || password == null || zhangHao.trim().isEmpty() || password.trim().isEmpty()) {
                map.put("code", 500);
                map.put("msg", "账号和密码不能为空");
                resp.getWriter().append(gson.toJson(map));
                return;
            }

            // 从数据库验证用户
            UserService userService = new UserService();
            User user = userService.login_check(zhangHao, password);

            if(user != null) {
                Map<String, Object> userData = new HashMap<>();
                userData.put("zhangHao", user.getZhangHao());
                userData.put("userName", "用户-" + user.getZhangHao());
                userData.put("userId", user.getUserId());
                userData.put("avatar", user.getAvatar());
                userData.put("role", "user");

                map.put("code", 200);
                map.put("msg", "登录成功");
                map.put("data", userData);
                System.out.println("普通用户登录成功: " + zhangHao);
            } else {
                map.put("code", 500);
                map.put("msg", "账号或密码错误");
                System.out.println("普通用户登录失败: " + zhangHao);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "服务器异常: " + e.getMessage());
        }

        String json = gson.toJson(map);
        resp.getWriter().append(json);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>用户登录服务</h1>");
        out.println("<p>请使用POST方法提交登录请求</p>");
        out.println("</body></html>");
    }
}